package com.qf.service.impl;

import com.qf.dao.OrderSettingMapper;
import com.qf.entity.OrderSetting;
import com.qf.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public List<Map> getOrderSettingByMonth(String month) {
        String begin = month + "-1"; //2022-8-1
        String end = month + "-31"; //2022-8-31
        //利用map存储
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);

        //根据日期范围查询预约设置数据
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if(list != null && list.size() > 0 ){
            for (OrderSetting orderSetting: list ) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());//获取日期数字，即几号
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());

                result.add(m);
            }
        }

        return result;
    }

    //根据日期设置对应的预约设置数据
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        //根据日期查询是否已经进行了预约设置
        Date orderDate = orderSetting.getOrderDate();

        long count = orderSettingMapper.findCountByOrderDate(orderDate);
        if (count > 0){
            //已经进行了预约设置，进行更新
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        }else{
            //没有进行预约设置，需要进行插入
            orderSettingMapper.add(orderSetting);
        }
    }
}
